package com.lej.core.binding.viewadapter.view;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.Log;
import android.view.View;

import androidx.databinding.BindingAdapter;

import com.lej.core.binding.command.BindingCommand;
import com.lej.core.widget.drawable.CustomDrawable;
import com.qmuiteam.qmui.kotlin.ViewKtKt;

import java.util.ArrayList;

import kotlin.Unit;

public class ViewAdapter {
    //防重复点击间隔(秒)
    public static final int CLICK_INTERVAL = 1;

    /**
     * requireAll 是意思是是否需要绑定全部参数, false为否
     * View的onClick事件绑定
     * onClickCommand 绑定的命令,
     * isThrottleFirst 是否开启防止过快点击
     */
    @BindingAdapter(value = {"onClickCommand", "isThrottleFirst"}, requireAll = false)
    public static void onClickCommand(View view, final BindingCommand clickCommand, final boolean isThrottleFirst) {
        ViewKtKt.onClick(view, 200, view1 -> {
            if (clickCommand != null) {
                clickCommand.execute();
            }
            return Unit.INSTANCE;
        });
    }

    /**
     * 设置圆角 边框 颜色等
     *
     * @param customDrawable 可以是CustomDrawable 也可以是带有选择状态的StateListDrawable
     */
    @BindingAdapter(value = {"customDrawable"}, requireAll = false)
    public static void setDrawable(View view, final Drawable customDrawable) {
        view.setBackground(customDrawable);
    }

    /**
     * view的显示隐藏
     */
    @BindingAdapter(value = {"isVisible"}, requireAll = false)
    public static void isVisible(View view, final Boolean visibility) {
        if (visibility != null && visibility) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }
    }

    /**
     * view的显示隐藏
     */
    @BindingAdapter(value = {"isGone"}, requireAll = false)
    public static void isGone(View view, final Boolean isGone) {
        if (isGone != null && isGone) {
            view.setVisibility(View.GONE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
    }

    /**
     * view的显示隐藏
     */
    @BindingAdapter(value = {"isInvisible"}, requireAll = false)
    public static void isInvisible(View view, final Boolean isInvisible) {
        if (isInvisible != null && isInvisible) {
            view.setVisibility(View.INVISIBLE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
    }

    /**
     * view的显示隐藏
     */
    @BindingAdapter(value = {"isSelected"}, requireAll = false)
    public static void isSelected(View view, final Boolean isSelected) {
        ArrayList<int[]> list = new ArrayList<>();
        view.setSelected(isSelected != null && isSelected);
    }


    /**
     * view的显示隐藏
     */
    @BindingAdapter(value = {"isEnable"}, requireAll = true)
    public static void isEnable(View view, final boolean isEnable) {
        view.setEnabled(isEnable);
    }
//    @BindingAdapter({"onTouchCommand"})
//    public static void onTouchCommand(View view, final ResponseCommand<MotionEvent, Boolean> onTouchCommand) {
//        view.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if (onTouchCommand != null) {
//                    return onTouchCommand.execute(event);
//                }
//                return false;
//            }
//        });
//    }
}
